package com.titanrise.crm.mapper;

import com.titanrise.crm.domain.model.ClientSalesRelationships;
import com.titanrise.crm.domain.model.User;
import com.titanrise.crm.environment.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: pront
 * @Time:2024-11-21 12:37
 */
@Mapper
public interface ClientSalesRelationshipsMapper {

    @ResultType(ClientSalesRelationships.class)
    @Select("select * from tb_client_sales_relationships")
    List<ClientSalesRelationships> selectAll();

    @Insert(value = "INSERT INTO tb_client_sales_relationships (id, client_id, sales_id) " +
            "VALUES (#{clientSalesRelationships.id}, #{clientSalesRelationships.clientId}, #{clientSalesRelationships.salesId})", autoGenerateUUID = true)
    int insertOne(@ParamEntity("clientSalesRelationships") ClientSalesRelationships clientSalesRelationships);

    @Delete("delete from tb_client_sales_relationships where sales_id = #{salesUid}")
    int deleteRelationshipsForSalesId(@Param("salesUid") String salesUid);

    @Select("select user.id from tb_client_sales_relationships rel " +
            "left join tb_user user on rel.client_id = user.id " +
            "where rel.sales_id = #{salesId}")
    @ResultType(User.class)
    List<User> selectAllRelativeUserForSalesId(@Param("salesId") String salesId);

    @Select("select sales_id from tb_client_sales_relationships where client_id = #{clientId}")
    String selectSalesIdByClientId(@Param("clientId") String clientId);

    @Delete("delete from tb_client_sales_relationships where client_id = #{clientId}")
    int deleteByClientId(@Param("clientId") String clientId);
}
